package com.ebt.m.data.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ebt.m.customer.entity.RiskBean;
import com.ebt.m.data.DatabaseManager;
import com.ebt.m.data.bean.CommonCustomer;
import com.ebt.m.data.db.Product;

import java.util.ArrayList;
import java.util.List;

/**
 * 险种百科所有的数据库提供类
 *
 * @author Michael.li
 */
public class WikiProvider {

    protected DatabaseManager dbManager;
    protected Context mContext;
    public WikiProvider(Context context) {
        mContext = context;
        dbManager = DatabaseManager.getInstance(context);
    }



    /**
     * 获取常用客户查询条件
     *
     * @param size
     * @return
     */
    public List<CommonCustomer> getCommonCustomerList(int size) {
        String sql = "select distinct age,sex,pro from customer_common order by updateTime desc limit " + size;
        return dbManager.sql2VOList(sql, new CommonCustomer());
    }

    public void saveCommonCustomer(CommonCustomer commonCustomer) {
        if (commonCustomer != null) {
            ContentValues cv = new ContentValues();
            cv.put("sex", commonCustomer.sex);
            cv.put("age", commonCustomer.age);
            cv.put("pro", commonCustomer.pro);
            cv.put("updateTime", commonCustomer.updateTime);
            cv.put("uuid", commonCustomer.uuid);
            dbManager.insert("customer_common", "", cv);
        }
    }

    /**
     * 根据品牌ID查询所有保险
     *
     * @param companyId
     * @return
     */
    public List<RiskBean> getRisksById(String companyId) {
        dbManager.open();

        String sql = "select "
                + Product.ID + ", "
                + Product.COLUMN_PRODUCTNAME + ", "
                + Product.COLUMN_SHORTNAME
                + " from " + Product.TABLE_NAME
                + " where " + Product.COLUMN_COMPANYID + "=" + companyId
                + " and " + Product.COLUMN_D_ISMAJOR + "=1;";

        Cursor c = dbManager.rawQuery(sql, null);
        dbManager.close();

        if (c == null) {
            return null;
        }

        List<RiskBean> data = new ArrayList<>();

        while (c.moveToNext()) {
            String id = c.getString(c.getColumnIndex(Product.ID));
            String name = c.getString(c.getColumnIndex(Product.COLUMN_PRODUCTNAME));
            String shortName = c.getString(c.getColumnIndex(Product.COLUMN_SHORTNAME));

            RiskBean risk = new RiskBean();
            risk.setId(id);
            risk.setName(name);
            risk.setShortName(shortName);
            data.add(risk);
        }

        c.close();
        return data;
    }

}
